//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using CommunityServer.Components;

namespace CommunityServer.Galleries.Components
{
	/// <summary>
	/// Summary description for GalleryPostCategory.
	/// </summary>
	public class GalleryPostCategory: CommunityServer.Components.PostCategory
	{
		public GalleryPostCategory()
		{
		}

		/// <summary>
		/// This does the lazy loading for the Featured Post and therefore should be called before trying to access the Post
		/// </summary>
		public bool HasFeaturedPhoto
		{
			get
			{
				if(!HasFeaturedPost)
					return false;
				else if(HasFeaturedPost && FeaturedPost == null)
				{
					try
					{
						FeaturedPost = GalleryPosts.GetPicture(FeaturedPostID);
					}
					catch{return false;}
				}


				if(!Permissions.ValidatePermissions(FeaturedPost.Section,Permission.Post, CSContext.Current.User))
					return ((GalleryPost)FeaturedPost).IsPostEnabled;
				else
					return true;
			}
		}

		/// <summary>
		/// Used to bind the thumbnail to the CA datagrid
		/// </summary>
		public string ThumbnailURL
		{
			get
			{
				if(HasFeaturedPhoto)
					return GalleryUrls.Instance().ThumbnailURL((GalleryPost)FeaturedPost);
				else
					return GalleryUrls.Instance().DefaultHighlightImage;

			}
		}

		/// <summary>
		/// Lazy loaded by HasFeaturedPhoto
		/// </summary>
		public override Post FeaturedPost
		{
			get
			{
					return base.FeaturedPost;
			}
			set
			{
				if(value != null && value.PostID > 0)
				{
					FeaturedPostID = value.PostID;
					base.FeaturedPost = value;
				}
				else
				{
					FeaturedPostID = -1;
					base.FeaturedPost = null;
				}
			}
		}

		public static GalleryPostCategory Parse(PostCategory postCategory)
		{
			GalleryPostCategory galleryPostCategory = new GalleryPostCategory();

			//		private int _categoryID;
			galleryPostCategory.CategoryID = postCategory.CategoryID;
			//		private int _featuredPostID =-1;
			galleryPostCategory.FeaturedPostID = postCategory.FeaturedPostID;
			//		private int _sectionID;
			galleryPostCategory.SectionID = postCategory.SectionID;
			//		private string _name;
			galleryPostCategory.Name = postCategory.Name;
			//		private bool _isEnabled;
			galleryPostCategory.IsEnabled = postCategory.IsEnabled;
			//		private int _parentID;
			galleryPostCategory.ParentID = postCategory.ParentID;
			//		private string _description;
			galleryPostCategory.Description = postCategory.Description;
			//		private string _path;
			galleryPostCategory.Path = postCategory.Path;
			//		private DateTime _dateCreated = SqlDateTime.MinValue.Value;
			galleryPostCategory.DateCreated = postCategory.DateCreated;
			//
			//		// Derived properties
			//		private int _totalThreads;
			galleryPostCategory.TotalThreads = postCategory.TotalThreads;
			//		private int _totalSubThreads;
			galleryPostCategory.TotalSubThreads = postCategory.TotalSubThreads;
			//		private DateTime _mostRecentPostDate = SqlDateTime.MinValue.Value;
			galleryPostCategory.MostRecentPostDate = postCategory.MostRecentPostDate;
			//		private DateTime _mostRecentSubPostDate = SqlDateTime.MinValue.Value;
			galleryPostCategory.MostRecentSubPostDate = postCategory.MostRecentSubPostDate;

			return galleryPostCategory;

		}
	}
}
